package cn.woblog.recyclerviewsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.OvershootInterpolator;
import java.util.ArrayList;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

public class AnimationFrameworkActivity extends AppCompatActivity {

  private ItemAnimationAdapter useCustomAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_animation_framework);

    RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));

    SlideInLeftAnimator animator = new SlideInLeftAnimator();
    animator.setInterpolator(new OvershootInterpolator());
//    animator.setAddDuration(700);
//    animator.setRemoveDuration(700);
// or recyclerView.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f));
    rv.setItemAnimator(animator);

    useCustomAdapter = new ItemAnimationAdapter(this);
    rv.setAdapter(useCustomAdapter);

    ArrayList<String> data = new ArrayList<>();
    for (int i = 0; i < 50; i++) {
      data.add("Item " + i);
    }

    useCustomAdapter.setData(data);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_animation_activity, menu);
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
//    int i = random.nextInt(useCustomAdapter.getItemCount() - 1);
    int i = 1;
    switch (item.getItemId()) {
      case R.id.menu_add:

        useCustomAdapter.addData(i, "Add " + i);
        break;
      case R.id.menu_delete:
        useCustomAdapter.removeData(i);
        break;

      case R.id.menu_refresh:
        useCustomAdapter.notifyItemChanged(i);
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
